// FunctionStack.h: interface for the FunctionStack class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_FUNCTIONSTACK_H__AD9ED56F_978F_42F4_81FC_A34E41D18840__INCLUDED_)
#define AFX_FUNCTIONSTACK_H__AD9ED56F_978F_42F4_81FC_A34E41D18840__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "map.h"
#include "FunctionObject.h"
#include "StackInterface.h"
class Part;

typedef StackInterface<INT , FunctionObject*,
 IteratorTemplate<FunctionObject*>*> FunctionStackInterface;
class FunctionStack;
class SameIDFunctionIterator;

class DllExport FunctionStackIterator : public FunctionStackInterface::Iterator
{
public:
	FunctionStackIterator(FunctionStack* pStack);
	virtual ~FunctionStackIterator();
	
public:
	virtual void Begin();
	virtual void Next();

private:
	POSITION m_pos;
	FunctionStack* m_pStack;
	SameIDFunctionIterator* m_pPrevFunctions;
};

class DllExport FunctionStack : public FunctionStackInterface
{
	friend FunctionStackIterator;
	friend SameIDFunctionIterator;
public:
	typedef CArray<DataType, DataType> SameIDFunctions;

public:
	Iterator* RemoveFunction(INT nID);
	void RemoveFunction(FunctionObject* pFunction);
	Iterator* GetFunction(INT nFunctionID);
	void AddFunction(FunctionObject* pFunction); 
	FunctionStack();
	virtual ~FunctionStack();

public:
	virtual void Add(const Index& index, const DataType& pType){
		AddFunction(pType);
	}

	virtual Iterator* Get(const Index& index){
		return GetFunction(index);
	}
	
	virtual Iterator* Remove(const Index& index){
		return RemoveFunction(index);
	}

	virtual void RemoveAll();
	virtual void ClearAll();
	void RegisterEffect();
	void RemoveEffect();

	virtual Iterator* NewIterator(){
		return new FunctionStackIterator(this);
	}

private:
	BOOL AddToSameIDFunctionArray(FunctionObject* pFunction, SameIDFunctions* pArr);
	BOOL IsExist(FunctionObject* pFunction, SameIDFunctions* pArr);

private:
	CMap<Index, Index, SameIDFunctions*, SameIDFunctions*> m_theFunctions;
	POSITION m_pos;
	DataType m_pPrevFunction;
};

class SameIDFunctionIterator : public FunctionStackInterface::Iterator
{
public:
	SameIDFunctionIterator(FunctionStack::SameIDFunctions* pSameIDFunctions, BOOL bIsDelFunctions = FALSE);
	virtual ~SameIDFunctionIterator(){
		if(m_bIsDelFunctions)
			delete m_pSameIDFunctions;
		m_pSameIDFunctions = NULL;
	}

public:
	virtual void Begin();
	virtual void Next();

private:
	INT m_nIndex;
	FunctionStack::SameIDFunctions* m_pSameIDFunctions;
	BOOL m_bIsDelFunctions;
};

#endif // !defined(AFX_FUNCTIONSTACK_H__AD9ED56F_978F_42F4_81FC_A34E41D18840__INCLUDED_)
